//
//  RZVideoPreviewViewController.m
//  RZIM
//
//  Created by reyzhang on 2021/8/30.
//

#import "RZVideoPreviewViewController.h"
#import "RZAVPlayer.h"
#import "RZKitCommonDefine.h"
#import "UIManager.h"


@interface RZVideoPreviewViewController ()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) RZAVPlayer *player;
@property (nonatomic, strong) UIButton *backBtn;
@end

@implementation RZVideoPreviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.backBtn];
    
    [self.player startAnimation];
    self.player.videoUrl = self.videoUrl;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}



- (void)dealloc
{
    [self.player stopPlayer];
    self.player = nil;
}

- (void)close:(id)sender {
    [self.player stopPlayer];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close:)];
        [_bgView addGestureRecognizer:tapGR];
    }
    return _bgView;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:RZResourceImage(@"navigator_white_back") forState:UIControlStateNormal];
        _backBtn.frame = CGRectMake(10, kSafeAreaTop + 20 , 44, 44);
        [_backBtn addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (RZAVPlayer *)player {
    if (!_player) {
        _player = [[RZAVPlayer alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) withShowInView:self.bgView url:nil];
    }
    return _player;
}

@end
